package com.t.bricks.bussiness.service.votestatistics.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.db.dao.votestatistics.VoteStatisticsDao;
import com.t.bricks.bussiness.db.entity.votestatistics.VoteStatistics;
import com.t.bricks.bussiness.model.votestatistics.ConditionVoteStatistics;
import com.t.bricks.bussiness.service.votestatistics.VoteStatisticsService;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.model.OrderInfoList;
import com.t.bricks.model.Page;
import com.t.bricks.utils.spring.ModuleUtil;

/**
 * 投票统计'VoteStatistics表'基本业务操作类接口实现
 */
@Service
public class VoteStatisticsServiceImpl implements VoteStatisticsService {

	@Autowired
	private VoteStatisticsDao voteStatisticsDao;
	
	/**
	 * 查询分页
	 * @param page 分页信息对象
	 * @param orders 排序数据集合
	 * @param sLikeStr 全文检索
	 * @param condition 查询条件对象
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findByPage(Page page, OrderInfoList orders, String sLikeStr,
			ConditionVoteStatistics condition) {
		if (null == page) {
			page = new Page(new ArrayList<VoteStatistics>());
		}

		return voteStatisticsDao.findByPage(page, orders, sLikeStr, condition);
	}

	/**
	 * 根据记录编号查询符合分页数据的某条记录
	 * @param sId
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findByPageRow(String sId) {
		if (null == sId || "".equals(sId.trim())) {
			return new MsgEmity(false, "编号不能为空！", 8001);
		}
		
		return voteStatisticsDao.findByPageRow(sId);
	}

	/**
	 * 投票
	 * @param sId
	 * @param sOption
	 * @return
	 */
	@Override
	public synchronized MsgEmity vote(String sId, String sOption) {
		if (null == sId || "".equals(sId.trim())) {
			return new MsgEmity(false, "编号不能为空！", 8001);
		}
		
		MsgEmity me = voteStatisticsDao.findVoteData(sId);
		if (!me.isSuccess()) {
			return me;
		}
		
		VoteStatistics voteStatistics = me.getData();
		
		String[] array = voteStatistics.getsUserIds().split(",");
		if (Arrays.asList(array).contains(ModuleUtil.currentLoginUserId())) {
			return new MsgEmity(false, "当前用户已经投票！", 8002);
		}
		
		String sUserIds = String.format("%s,%s", voteStatistics.getsUserIds(), ModuleUtil.currentLoginUserId())
				.replaceAll("null", "");
		String sTotalOptions = String.format("%s,%s", voteStatistics.getsTotalOptions(), sOption)
				.replaceAll("null", "");

		Map<String, Integer> map = new HashMap<String, Integer>();
		for (String string : sTotalOptions.split(",")) {
			if (null == string || "".equals(string.trim())) {
				continue;
			}
			
			map.put(string, map.get(string) + 1);
		}

		StringBuffer sb = new StringBuffer();
		for (Map.Entry<String, Integer> entry : map.entrySet()) {
			sb.append(entry.getKey()).append(":").append(entry.getValue()).append(";");
		}
		
		return voteStatisticsDao.vote(sId, sUserIds, sb.toString(), sTotalOptions);
	}

}
